//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.github.pagehelper.PageHelper;
import com.xw.common.Const;
import com.xw.dao.HistoryUploadVideosMapper;
import com.xw.dao.VideosItemMapper;
import com.xw.dao.VideosMapper;
import com.xw.model.HistoryUploadVideos;
import com.xw.model.Videos;
import com.xw.model.VideosItem;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.vo.VideosItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class VideosItemServiceImpl implements VideosItemService {

    @Resource
    private VideosItemMapper videosItemMapper;
    @Resource
    private VideosMapper videosMapper;
    @Resource
    private HistoryUploadVideosMapper uploadVideosMapper;

    @Override
    public Map findAllByVideoIdJson(Integer page, Integer limit, String videoId, String videoItemName, Integer videoStatus) {
        Integer count=videosItemMapper.findAllByVideoIdJson(videoId,videoItemName,videoStatus).size();

        if (page != null && limit !=null){
            PageHelper.startPage(page,limit,false);
        }
        List<VideosItemVo> list =videosItemMapper.findAllByVideoIdJson(videoId,videoItemName,videoStatus);
        return JsonData.returnLayuiMap(list,count);
    }

    @Override
    public void updateVideosItem(String videoId, List<VideosItem> list) {
        videosItemMapper.updateVideosItem(videoId,list);
    }

    @Override
    public Map findVideoItemByVideoId(String videoId) {
        VideosItem item = new VideosItem();
        item.setVideoId(videoId);
        PageHelper.orderBy("sort asc");
        List<VideosItem> list = videosItemMapper.select(item);
        return JsonData.returnLayuiMap(list,list.size());
    }

    @Override
    public Map findItemByVideoId(String userId, String videoId) {
        VideosItem model = new VideosItem();
        model.setVideoId(videoId);
        PageHelper.orderBy("sort asc");
        List<VideosItem> list = videosItemMapper.select(model);
        return JsonData.success(list);
    }

    @Override
    public Map findAllJson(Integer page, Integer limit, String videoId, String videoItemName, Integer videoStatus) {
        Integer count=videosItemMapper.findByWhere(videoId,videoItemName,videoStatus).size();

        if (page != null && limit !=null){
            PageHelper.startPage(page,limit,false);
        }
        List<VideosItemVo> list =videosItemMapper.findByWhere(videoId,videoItemName,videoStatus);
        return JsonData.returnLayuiMap(list,count);
    }

    @Override
    public Map setTopVideo(String videoId, Integer isTop) {
        Videos videosUpdate = this.videosMapper.selectByPrimaryKey(videoId);
        if(null == videosUpdate){
            return JsonData.fail("该课程不存在");
        }
        videosUpdate.setIsTop(isTop == 0 ? 0:1);
        int i = this.videosMapper.updateByPrimaryKey(videosUpdate);
        return JsonData.returnMap(i);
    }

    @Override
    /**
     * @MethodName: restartSortVideoItems
     * @Description: 重新排序
     * @author: moke
     * @createTime: 2019/12/16 13:42
     * @param videosItemList
     * @Return: java.util.Map
     */
    public Map restartSortVideoItems(List<VideosItem> videosItemList) {
        for (VideosItem videosItem : videosItemList) {
            if(StringUtils.isEmpty(videosItem.getVideoItemId())){
                return JsonData.fail("视频主键不能为空");
            }
            this.videosItemMapper.updateByPrimaryKeySelective(videosItem);
        }
        return JsonData.success();
    }

    @Override
    public Map setTopVideoItem(String videoItemId, Integer isTop) {
        /*获取更新的视频对象*/
        VideosItem videosItem = this.videosItemMapper.selectByPrimaryKey(videoItemId);
        if(null == videosItem){
            return JsonData.fail("该视频不存在");
        }
        videosItem.setIsTop(0 == isTop ? 0:1);
        int i = this.videosItemMapper.updateByPrimaryKeySelective(videosItem);
        return JsonData.returnMap(i);
    }

    @Override
    public Map reeditVideoItem(String videoItemId, String videoUrl, String videoItemName, Integer sort, Double videoItemSize) {
        VideosItem videosItem = this.videosItemMapper.selectByPrimaryKey(videoItemId);
        if(null != videoItemName){
            videosItem.setVideoItemName(videoItemName);
        }
        if(null != videoUrl){
            videosItem.setVideoUrl(videoUrl);
        }
        if(null != videoUrl){
            videosItem.setSort(sort);
        }
        if(null != videoItemSize){
            videosItem.setVideoItemSize(videoItemSize);
        }
        int i = this.videosItemMapper.updateByPrimaryKeySelective(videosItem);
        return JsonData.returnMap(i);
    }

    @Override
    public Map pass(String videoItemId, String videoId) {
        //修改状态
        VideosItem item = new VideosItem();
        item.setVideoItemId(videoItemId);
        item.setVideoStatus(1);
        item.setUpdateTime(new Date());
        videosItemMapper.updateByPrimaryKeySelective(item);
        Videos videos = videosMapper.selectByPrimaryKey(videoId);
        if ( null != videos){

            Integer publishType = videos.getPublishType();

            //查询所有视频数
            VideosItem vo = new VideosItem();
            vo.setVideoId(videoId);
            int count1 = videosItemMapper.selectCount(vo);

            vo.setVideoStatus(1);
            int count2 = videosItemMapper.selectCount(vo);

            //之前是发布状态，后台添加视频还是发布状态,之前不是发布状态，添加视频还是未发布状态
            if (videos.getIsPublish() != 1) {
                //判断通过的视频数和总视频数是否相等，相等就修改发布状态
                if (publishType == 1) {
                    //发布类型 1.审核通过后立即发布 2.先提交本次审核
                    if (count1 == count2) {
                        //判断课程信息是否审核通过
                        Videos videos1 = new Videos();
                        videos1.setVideoId(videoId);
                        videos1.setCheckStatus(1);
                        int count = videosMapper.selectCount(videos1);
                        if (count != 0) {
                            //全部审核通过，修改状态
                            videos.setIsPublish(1);
                            videos.setUpdateTime(new Date());
                            videosMapper.updateByPrimaryKeySelective(videos);
                        }
                    }
                }
            }
        }


        return JsonData.returnMap(1);
    }

    @Override
    public Map notPass(String videoItemId, String videoId, String noPassReason) {
//        //不通过之后，如果之前是发布状态就要将发布状态修改为未发布状态
//        Videos videos = videosMapper.selectByPrimaryKey(videoId);
//        Integer isPublish = videos.getIsPublish();
//        if (isPublish == 1){
//            videos.setIsPublish(0);
//            videosMapper.updateByPrimaryKeySelective(videos);
//        }

        VideosItem item = new VideosItem();
        item.setVideoItemId(videoItemId);
        item.setNoPassReason(noPassReason);
        item.setVideoStatus(2);
        item.setUpdateTime(new Date());
        int flag = videosItemMapper.updateByPrimaryKeySelective(item);
        return JsonData.returnMap(flag);
    }

    @Override
    public List<VideosItemVo> findByWhere(Integer page, Integer limit, String videoId) {

        if (page >0 & limit >0){
            PageHelper.startPage(page,limit,false);
            PageHelper.orderBy("create_time desc");
        }
        List<VideosItemVo> videosItemVoList = videosItemMapper.findByWhere(videoId,null,null);
        return videosItemVoList;
    }


    /**
     * @需求描述:添加视频到课程
     * @创建人:whh
     * @创建时间: 2019/11/23
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map saveVideoItemToVideo(String userId, String videoId, String videoItemId) {

        VideosItem videosItem = videosItemMapper.selectByPrimaryKey(videoItemId);
        if (null == videosItem){
            return JsonData.fail("视频不存在");
        }
        videosItem.setUserId(userId);
        videosItem.setVideoId(videoId);
        Integer result = videosItemMapper.updateByPrimaryKey(videosItem);
        if (1 == result){
            return  JsonData.success(result,Const.OPREATE_MSG_SUCCESS);
        }
        return JsonData.fail(Const.OPREATE_MSG_ERROR);
    }


    @Override
    public Map insertVideoItem(String videoItemName, String videoItemUrl, String videoId, String userId, Integer sort) {

        VideosItem videosItem = new VideosItem();
        videosItem.setVideoId(videoId);
        videosItem.setVideoUrl(videoItemUrl);
        videosItem.setVideoItemName(videoItemName);
        videosItem.setVideoItemId(AppUtil.uuid());
        videosItem.setUserId(userId);
        videosItem.setVideoStatus(1);
        videosItem.setSort(sort);
        videosItem.setCreateTime(new Date());
        int insert = videosItemMapper.insertSelective(videosItem);
        HistoryUploadVideos uploadVideos = new HistoryUploadVideos();
        uploadVideos.setHuvId(AppUtil.uuid());
        uploadVideos.setCreateTime(new Date());
        uploadVideos.setUploadCount(1);
        uploadVideos.setUserId(userId);
        uploadVideos.setVideoId(videoId);
        uploadVideosMapper.insertSelective(uploadVideos);

        return JsonData.success(insert);
    }
}
